package com.torry.workflow.common.base.result;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Result<T> {
    private int code;
    private String msg;

    private T data;

    public static <T> Result<T> success() {
        Result result = new Result();
        result.setCode(200);
        result.setData(null);
        result.setMsg("操作成功");
        return result;
    }

    public static <T> Result<T> success(String msg, T data) {
        Result result = new Result();
        result.setCode(200);
        result.setData(data);
        result.setMsg(msg);
        return result;
    }

    public static <T> Result<T> success(T data) {
        Result result = new Result();
        result.setCode(200);
        result.setData(data);
        result.setMsg("操作成功");
        return result;
    }


    public static <T> Result<T> fail() {
        Result result = new Result();
        result.setCode(400);
        result.setData(null);
        result.setMsg("操作失败");
        return result;
    }

    public static <T> Result<T> fail(String msg) {
        Result result = new Result();
        result.setCode(400);
        result.setData(null);
        result.setMsg(msg);
        return result;
    }

    public static <T> Result<T> fail(int code, String msg) {
        Result result = new Result();
        result.setCode(code);
        result.setData(null);
        result.setMsg(msg);
        return result;
    }


    /**
     * 返回信息 手动封装（通用）
     *
     * @author wtao
     * @date 2021-10-28 19:51
     */
    public static <T> Result<T> ok(int code, String msg, T data) {
        return new Result(code, msg, data);
    }

    /**
     * 查询成功返回提示信息 （不分页数据）
     *
     * @author wtao
     * @date 2021-10-28 19:47
     */
    public static <T> Result<T> selectSuccess(T data) {
        return new Result(200, "查询成功", data);
    }

    /**
     * 保存成功返回提示信息
     *
     * @author wtao
     * @date 2021-10-28 19:47
     */
    public static Result saveSuccess() {
        return new Result(200, "保存成功", null);
    }

    /**
     * 删除成功返回提示信息
     *
     * @author wtao
     * @date 2021-10-28 19:47
     */
    public static Result delSuccess() {
        return new Result(200, "删除成功", null);
    }


    public static <T>PageResult selectPageSuccess(IPage<T> page) {
        PageResult result = new PageResult();

        result.setCode(200);
        result.setMsg("分页查询成功");

        result.setPageNum(page.getCurrent());
        result.setPageSize(page.getSize());
        result.setTotal(page.getTotal());
        result.setData(page.getRecords());

        return result;
    }
}
